package cc.zy.base.hr.controller;

import cc.zy.base.businesses.entity.College;
import cc.zy.base.common.controller.BaseController;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.DateUtil;
import cc.zy.base.common.utils.FebsUtil;
import cc.zy.base.hr.entity.StaffSyncLog;
import cc.zy.base.hr.service.IStaffSyncLogService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @author ljy
 * @time 2022/03/09 21:26:30
 */
@Controller("staffSyncLogView")
@Slf4j
@Validated
@RequiredArgsConstructor
public class StaffSyncLogController extends BaseController {


    @Autowired
    private final IStaffSyncLogService logService;

    /**
     * 打开详情页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "staffSyncLog/detail/{id}")
    public String staffSyncLogDetail(@PathVariable Integer id, Model model) {
        resolveStaffModel(id, model, true);
        return FebsUtil.hrView("staff/staffSyncLogDetail");
    }

    /**
     * 查找员工同步日志
     *
     * @param request
     * @param staffSyncLog
     * @return
     */
    @GetMapping("staffSyncLog/list")
    @ResponseBody
    public FebsResponse staffSyncLogList(QueryRequest request, StaffSyncLog staffSyncLog) {
        log.debug("前台传来的对象是：" + staffSyncLog);
        Map<String, Object> dataTable = getDataTable(logService.getStaffSyncLogs(request, staffSyncLog));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 修改更新的数据格式,将数据放到页面里
     */
    private void resolveStaffModel(Integer id, Model model, Boolean transform) {
        StaffSyncLog staffSyncLog = logService.getStaffSyncLogById(id);

        if (staffSyncLog.getSyncTime() != null) {
            model.addAttribute("syncDateTime", DateUtil.getDateFormat(staffSyncLog.getSyncTime(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }

        model.addAttribute("staffSyncLog", staffSyncLog);
    }

    @PostMapping("staffSyncLog/download")
    @ResponseBody
    public void download(@RequestBody String json, HttpServletResponse response) {
        StaffSyncLog staffSyncLog = JSON.parseObject(json,StaffSyncLog.class);
        try {
            // 读到流中
            // 文件的存放路径
            InputStream inputStream = new FileInputStream(staffSyncLog.getSyncUrl());
            response.reset();
            response.setContentType("application/octet-stream");
            String filename = new File(staffSyncLog.getSyncUrl()).getName();
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            //从输入流中读取一定数量的字节，并将其存储在缓冲区字节数组中，读到末尾返回-1
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException ex) {
            log.error(ex.getMessage());
        }
    }

}
